"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M*数据库show privileges语法
Case Name   : show privileges语法显示的权限delete权限验证
Create At   : 2022.11.18
Owner       : z@wan005
Description :
    1、创建兼容b库
    2、建表并插入数据
    3、创建用户
    4、回收用户delete权限
    5、删除表数据
    6、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、报错，权限拒绝
    6、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class CompatibilityTest0006(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_privileges_0006"
        self.tb_name = "tb_privileges_0006"
        self.us_name = "us_privileges_0006"

    def test_privileges(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----step1:创建兼容b库;expect:成功---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step2:建表并插入数据;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name};" \
                  f"create table {self.tb_name}(id int,name varchar(10));" \
                  f"insert {self.tb_name} values (1,'小明');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS,
                      sql_res, '执行失败' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      sql_res, '执行失败' + text)

        text = '--step3:创建用户;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop user if exists {self.us_name};" \
                  f"create user {self.us_name} " \
                  f"password '{macro.COMMON_PASSWD}';"
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, sql_res,
                      '执行失败' + text)

        text = '--step4:回收用户delete权限;expect:成功--'
        self.log.info(text)
        sql_cmd = f"revoke delete on {self.tb_name} from {self.us_name};"
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn(self.constant.REVOKE_SUCCESS_MSG, sql_res,
                      '执行失败' + text)

        text = '--step5:删除表数据;expect:报错，权限拒绝--'
        self.log.info(text)
        sql_cmd = f" delete from {self.tb_name};"
        connect_user = f'-U {self.us_name} ' \
                       f'-W {macro.COMMON_PASSWD}'
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                sql_type=connect_user,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn(f'ERROR:  permission denied for relation {self.tb_name}',
                      sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step6:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop user {self.us_name} cascade;"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname=f'{self.db_name}')
        self.log.info(drop_res1)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res2 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res2)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, drop_res1,
                      '执行失败' + text)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res2,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
